package isp.apps.example.shoppingcart.domain;

import java.math.BigDecimal;

import java.util.Iterator;
import java.util.List;

public class GraduatedShippingCalculator implements ShippingCalculator
{
  private BigDecimal rate;
  private int itemCount = 0;

  public GraduatedShippingCalculator()
  {
  }

  public BigDecimal getShippingCost(List cartList, ShippingOption so)
  {
    itemCount = 0;
    CartItem ci = new CartItem();
    for (Iterator i = cartList.iterator(); i.hasNext(); )
    {
      ci = (CartItem)i.next();
      itemCount = itemCount + ci.getQuantity();
    }
    
    rate = so.getRate();
    //rate = rate + rate * (count / 10)
    return rate.add(rate.multiply(new BigDecimal(itemCount).divide(rate, BigDecimal.ROUND_HALF_UP)));
  }
}
